package com.mvetc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.mvetc.dao.CommentDao;
import com.mvetc.entity.Comment;
import com.mvetc.entity.Movie;
import com.mvetc.entity.PageInfo;
import com.mvetc.entity.User;
import com.mvetc.util.DBUtil;

public class CommentDaoImpl implements CommentDao {

	@Override
	public PageInfo<Comment> findBy(Map<String, String> params) {
		int pageNum = Integer.parseInt(params.get("pageNum"));
		int pageSize = Integer.parseInt(params.get("pageSize"));
		List<Comment> list = new ArrayList<>();
		ResultSet doQuery = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select A.comment_id")
		.append(",A.comment_content")
		.append(",A.comment_date")
		.append(",A.user_id")
		.append(",A.movie_id")
		.append(",B.user_name")
		.append(",C.movie_name")
		.append(" from comment_info A inner join user_info B")
		.append(" on A.user_id = B.user_id")
		.append(" inner join movie_info C")
		.append(" on A.movie_id = C.movie_id");
		if (!"".equals(params.get("movieNameKey")) ) {
			sb.append(" where C.movie_name like ? order by A.comment_date asc limit ?,?");
			doQuery = DBUtil.doQuery(sb.toString(), "%"+params.get("movieNameKey")+"%",(pageNum-1)*pageSize,pageSize);
		}else if ( !"".equals(params.get("userNameKey"))) {
			sb.append(" where B.user_name like ? order by A.comment_date asc  limit ?,?");
			doQuery = DBUtil.doQuery(sb.toString(), "%"+params.get("userNameKey")+"%",(pageNum-1)*pageSize,pageSize);
		}else if ( !"".equals(params.get("movieId"))) {
			sb.append(" where A.movie_id = ?");
			doQuery = DBUtil.doQuery(sb.toString(), params.get("movieId"));
		}else {
			sb.append(" order by A.comment_date asc limit ?,?");
			doQuery = DBUtil.doQuery(sb.toString(),(pageNum-1)*pageSize,pageSize);
		}
		try {
			while (doQuery.next()) {
				String commentId = doQuery.getString("comment_id");
				String commentContent = doQuery.getString("comment_content");
				String commentDate = doQuery.getString("comment_date");
				String userId = doQuery.getString("user_id");
				String userName = doQuery.getString("user_name");
				String movieId = doQuery.getString("movie_id");
				String movieName = doQuery.getString("movie_name");
				User user = new User(userId, userName, null, null, null, null, null);
				Movie movie = new Movie(movieId, movieName, null, null, null, null, null, null, null, null, null, null, null, null, null);
				Comment comment = new Comment(commentId, commentContent, commentDate, user, movie);
				list.add(comment);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		Integer total = 0;
		// 获取符合当前查询条件的总记录数
		doQuery = null;
		if (!"".equals(params.get("movieNameKey"))) {
			doQuery = DBUtil.doQuery("select count(*) from comment_info A inner join movie_info B on A.movie_id = B.movie_id where movie_name like ?","%"+params.get("movieNameKey")+"%");
		}else if (!"".equals(params.get("userNameKey"))) {
			doQuery = DBUtil.doQuery("select count(*) from comment_info A inner join user_info B on A.user_id = B.user_id where user_name like ?","%"+params.get("userNameKey")+"%");
		}else {
			doQuery = DBUtil.doQuery("select count(*) from comment_info");
		}
		
		try {
			while (doQuery.next()) {
				total = doQuery.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		PageInfo<Comment> pageInfo = new PageInfo<>(list, pageNum, pageSize, total);
		return pageInfo;
	}

}
